package com.gui.cmn.controller;

import com.gui.cmn.service.DictService;
import com.gui.common.result.Result;
import com.gui.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author GodLamp
 * @date 2022/10/15 16:07
 */
@RestController
@Api(tags = "数据字典接口")
@RequestMapping(path = "/admin/cmn/dict")
public class DictController {
    @Resource
    private DictService dictService;

    @ApiOperation(value = "根据上级id查询子数据列表")
    @GetMapping(path = "findChildData/{id}")
    public Result findChildData(@PathVariable(name = "id") Long id){
        List<Dict> list = dictService.findChildData(id);

        return Result.ok(list);
    }

    /**
     * 导出数据无需返回值
     * @param response
     */
    @ApiOperation(value = "导出数据字典的数据")
    @GetMapping(path = "exportDict")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    @ApiOperation(value = "导入数据字典的数据")
    @PostMapping(path = "importDict")
    public Result importDictData(MultipartFile file){
        dictService.importDictData(file);

        return Result.ok();
    }

    /**
     * 根据dictCode与value查询
     */
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(path = "getName/{dictCode}/{value}")
    public String getName(@ApiParam(name = "dictCode", value = "上级编码", required = true) @PathVariable(name = "dictCode") String dictCode,
                          @ApiParam(name = "value", value = "值", required = true) @PathVariable(name = "value") String value){

        String dictName = dictService.getDictName(dictCode, value);

        return dictName;
    }

    /**
     * 根据value查询
     */
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(path = "getName/{value}")
    @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "String", paramType = "path")
    public String getName(@ApiParam(name = "value", value = "值", required = true) @PathVariable(name = "value") String value){

        String dictName = dictService.getDictName(value);

        return dictName;
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(path = "findByDictCode/{dictCode}")
    public Result findByDictCode(@ApiParam(name = "dictCode", value = "节点编码", required = true) @PathVariable(name = "dictCode") String dictCode){

        List<Dict> list = dictService.findByDictCode(dictCode);

        return Result.ok(list);
    }
}
